home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C25 / FunctionStaticSingleton.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  622 b   |  30 lines

  1. //: C25:FunctionStaticSingleton.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6.  
  7. class Singleton1 {
  8.   Singleton1() {}
  9. public:
  10.   static Singleton1& ref() {
  11.     static Singleton1 single;
  12.     return single;
  13.   }
  14. };
  15.  
  16. class Singleton2 {
  17.   Singleton1& s1;
  18.   Singleton2(Singleton1& s) : s1(s) {}
  19. public:
  20.   static Singleton2& ref() {
  21.     static Singleton2 single(Singleton1::ref());
  22.     return single;
  23.   }
  24.   Singleton1& f() { return s1; }
  25. };
  26.  
  27. int main() {
  28.   Singleton1& s1 = Singleton2::ref().f();
  29. } ///:~
  30.